using System.Collections.Generic;
using System.Text;
namespace System.Text
{
	/// <summary>The Base32 class</summary>
	/// <created author="laurentiu.macovei" date="Wed, 28 Dec 2011 00:35:43 GMT"/>
	public static class Base32
	{

		
		#region Fields


		#region Static

		public static string ValidChars = "QAZ2WSX3" + "EDC4RFV5" + "TGB6YHN7" + "UJM8K9LP";

		#endregion Static


		#endregion Fields


		#region Business Methods

		/// <summary>Converts an array of bytes to a Base32-k string.</summary>
		/// <created author="laurentiu.macovei" date="Wed, 28 Dec 2011 00:35:44 GMT"/>
		public static string ToBase32String(params byte[] bytes)
		{
			if (bytes == null || bytes.Length == 0)
				return string.Empty;
			StringBuilder sb = new StringBuilder();         // holds the base32 chars
			byte index;
			int hi = 5;
			int currentByte = 0;

			while (currentByte < bytes.Length)
			{
				// do we need to use the next byte?
				if (hi > 8)
				{
					// get the last piece from the current byte, shift it to the right
					// and increment the byte counter
					index = (byte)(bytes[currentByte++] >> (hi - 5));
					if (currentByte != bytes.Length)
					{
						// if we are not at the end, get the first piece from
						// the next byte, clear it and shift it to the left
						index = (byte)(((byte)(bytes[currentByte] << (16 - hi)) >> 3) | index);
					}

					hi -= 3;
				}
				else if (hi == 8)
				{
					index = (byte)(bytes[currentByte++] >> 3);
					hi -= 3;
				}
				else
				{

					// simply get the stuff from the current byte
					index = (byte)((byte)(bytes[currentByte] << (8 - hi)) >> 3);
					hi += 5;
				}

				sb.Append(ValidChars[index]);
			}

			return sb.ToString();
		}

		/// <summary>Converts a Base32-k string into an array of bytes.</summary>
		/// <exception cref="System.ArgumentException">
		/// Input string <paramref name="str">str</paramref>contains invalid Base32-k characters. 
		/// </exception>
		/// <created author="laurentiu.macovei" date="Wed, 28 Dec 2011 00:35:44 GMT"/>
		public static byte[] FromBase32String(string str)
		{
			int numBytes = str.Length * 5 / 8;
			byte[] bytes = new Byte[numBytes];

			// all UPPERCASE chars
			str = str.ToUpper();

			int bit_buffer;
			int currentCharIndex;
			int bits_in_buffer;

			if (str.Length < 3)
			{
				bytes[0] = (byte)(ValidChars.IndexOf(str[0]) | ValidChars.IndexOf(str[1]) << 5);
				return bytes;
			}

			bit_buffer = (ValidChars.IndexOf(str[0]) | ValidChars.IndexOf(str[1]) << 5);
			bits_in_buffer = 10;
			currentCharIndex = 2;
			for (int i = 0; i < bytes.Length; i++)
			{
				bytes[i] = (byte)bit_buffer;
				bit_buffer >>= 8;
				bits_in_buffer -= 8;
				while (bits_in_buffer < 8 && currentCharIndex < str.Length)
				{
					bit_buffer |= ValidChars.IndexOf(str[currentCharIndex++]) << bits_in_buffer;
					bits_in_buffer += 5;
				}
			}

			return bytes;
		}


		#endregion Business Methods


		// the valid chars for the encoding



	}
}